
#include <sstream>
#include "cmd_obj.h"
#include "cgi_stub_cntl.h"
#include "companydao_stub4web.h"
#include "companydao_req_resp.h"


using namespace erp::company::dao;

	uint32_t CCompanyDaoStub4Web::AddAndUpdateCompany(
				const CCntlInfo& rCntlInfo,
				const std::string& machineKey,
				const std::string& source,
				uint32_t sceneId,
				const erp::company::ddo::CCompanyInfoDdo& companyInfo,
				const std::string& inReserve,
				std::string& errmsg,
				std::string& outReserve
	)
	{
		CCgiStubCntl oCgiStubCntl;
		oCgiStubCntl.Initialize_r();
		this->SetCgiStubCntl(&oCgiStubCntl);
	
	    CCompanyDaoAddAndUpdateCompanyReq oReq;
			oReq.machineKey = machineKey;
			oReq.source = source;
			oReq.sceneId = sceneId;
			oReq.companyInfo = companyInfo;
			oReq.inReserve = inReserve;
			oReq.errmsg = errmsg;
			oReq.outReserve = outReserve;

		// oCgiStubCntl.setRouteKey(uint32_t routeKey); 娌℃湁璁剧疆routeKey  
	
	    oCgiStubCntl.setPassport(
	        rCntlInfo.getOperatorUin(), 
	        rCntlInfo.getOperatorKey().c_str(),
	        rCntlInfo.getOperatorClientIP());
	
		if(const_cast<CCntlInfo&>(rCntlInfo).isRouteKeySet())	
			oCgiStubCntl.setUin(const_cast<CCntlInfo&>(rCntlInfo).getRouteKey());	
	    
	    int iRetCode = 0;
		m_sErrMsg.clear();
	    if((iRetCode = oCgiStubCntl.putSvcObj(&oReq, 0)) != 0)
	    {
			m_sErrMsg = "DoRequest failed:";
			m_sErrMsg += CCGI_STUB_CNTL->GetLastErrMsg();
	        return iRetCode;
	    }
	    
	    // Send & Recv
		if((iRetCode = SendRecv()) != 0)
		{
			if(99 == iRetCode) // UDP & Need No Response
				return 0;
	
			std::stringstream oss;
			oss << m_lTimeout;
	        m_sErrMsg = "Send & Recv Timeout(";
			m_sErrMsg += oss.str();
			m_sErrMsg += "s) ";
			m_sErrMsg += oCgiStubCntl.GetLastErrMsg();
			return iRetCode;
	    }
	    
		if(!m_poCmd->isValid(COMPANYDAO_ADDANDUPDATECOMPANY_RESP_CMD_ID))
	    {
	        std::stringstream oss;
            oss << "Cmd not match: "
                << "Expected cmd=" << std::hex << COMPANYDAO_ADDANDUPDATECOMPANY_RESP_CMD_ID << std::dec << " != "
                << "actualed cmd=" << std::hex << m_poCmd->getCmdId() << std::dec;
            m_sErrMsg = oss.str();
        
	        return 0xEE0024;
	    }
	    
	    char* pBuf = NULL; uint32_t nLen = 0;
	    m_poCmd->getBuffer(pBuf, nLen);
	    CByteStreamNetwork bs(pBuf, nLen);
	    bs.isStoring(false);
	    
	    CCompanyDaoAddAndUpdateCompanyResp oResp;
	    if(!oResp.Serialize(bs))
	    {
	        m_sErrMsg = "Resp.Serialize(bs) failed";
	        return 0xEE0028;
	    }
	    
			errmsg  = oResp.errmsg;
			outReserve  = oResp.outReserve;
	    
	    return oResp.dwResult;
	}
	uint32_t CCompanyDaoStub4Web::DeleteCompany(
				const CCntlInfo& rCntlInfo,
				const std::string& machineKey,
				const std::string& source,
				uint32_t sceneId,
				const erp::company::ddo::CCompanyInfoDdo& companyInfo,
				const std::string& inReserve,
				std::string& errmsg,
				std::string& outReserve
	)
	{
		CCgiStubCntl oCgiStubCntl;
		oCgiStubCntl.Initialize_r();
		this->SetCgiStubCntl(&oCgiStubCntl);
	
	    CCompanyDaoDeleteCompanyReq oReq;
			oReq.machineKey = machineKey;
			oReq.source = source;
			oReq.sceneId = sceneId;
			oReq.companyInfo = companyInfo;
			oReq.inReserve = inReserve;
			oReq.errmsg = errmsg;
			oReq.outReserve = outReserve;

		// oCgiStubCntl.setRouteKey(uint32_t routeKey); 娌℃湁璁剧疆routeKey  
	
	    oCgiStubCntl.setPassport(
	        rCntlInfo.getOperatorUin(), 
	        rCntlInfo.getOperatorKey().c_str(),
	        rCntlInfo.getOperatorClientIP());
	
		if(const_cast<CCntlInfo&>(rCntlInfo).isRouteKeySet())	
			oCgiStubCntl.setUin(const_cast<CCntlInfo&>(rCntlInfo).getRouteKey());	
	    
	    int iRetCode = 0;
		m_sErrMsg.clear();
	    if((iRetCode = oCgiStubCntl.putSvcObj(&oReq, 0)) != 0)
	    {
			m_sErrMsg = "DoRequest failed:";
			m_sErrMsg += CCGI_STUB_CNTL->GetLastErrMsg();
	        return iRetCode;
	    }
	    
	    // Send & Recv
		if((iRetCode = SendRecv()) != 0)
		{
			if(99 == iRetCode) // UDP & Need No Response
				return 0;
	
			std::stringstream oss;
			oss << m_lTimeout;
	        m_sErrMsg = "Send & Recv Timeout(";
			m_sErrMsg += oss.str();
			m_sErrMsg += "s) ";
			m_sErrMsg += oCgiStubCntl.GetLastErrMsg();
			return iRetCode;
	    }
	    
		if(!m_poCmd->isValid(COMPANYDAO_DELETECOMPANY_RESP_CMD_ID))
	    {
	        std::stringstream oss;
            oss << "Cmd not match: "
                << "Expected cmd=" << std::hex << COMPANYDAO_DELETECOMPANY_RESP_CMD_ID << std::dec << " != "
                << "actualed cmd=" << std::hex << m_poCmd->getCmdId() << std::dec;
            m_sErrMsg = oss.str();
        
	        return 0xEE0024;
	    }
	    
	    char* pBuf = NULL; uint32_t nLen = 0;
	    m_poCmd->getBuffer(pBuf, nLen);
	    CByteStreamNetwork bs(pBuf, nLen);
	    bs.isStoring(false);
	    
	    CCompanyDaoDeleteCompanyResp oResp;
	    if(!oResp.Serialize(bs))
	    {
	        m_sErrMsg = "Resp.Serialize(bs) failed";
	        return 0xEE0028;
	    }
	    
			errmsg  = oResp.errmsg;
			outReserve  = oResp.outReserve;
	    
	    return oResp.dwResult;
	}
	uint32_t CCompanyDaoStub4Web::GetCompany(
				const CCntlInfo& rCntlInfo,
				const std::string& machineKey,
				const std::string& source,
				uint32_t sceneId,
				const erp::company::ddo::CCompanyFilterDdo& companyFilter,
				const std::string& inReserve,
				std::string& errmsg,
				std::vector<erp::company::ddo::CCompanyInfoDdo >& companyInfo,
				std::string& outReserve
	)
	{
		CCgiStubCntl oCgiStubCntl;
		oCgiStubCntl.Initialize_r();
		this->SetCgiStubCntl(&oCgiStubCntl);
	
	    CCompanyDaoGetCompanyReq oReq;
			oReq.machineKey = machineKey;
			oReq.source = source;
			oReq.sceneId = sceneId;
			oReq.companyFilter = companyFilter;
			oReq.inReserve = inReserve;
			oReq.errmsg = errmsg;
			oReq.companyInfo = companyInfo;
			oReq.outReserve = outReserve;

		// oCgiStubCntl.setRouteKey(uint32_t routeKey); 娌℃湁璁剧疆routeKey  
	
	    oCgiStubCntl.setPassport(
	        rCntlInfo.getOperatorUin(), 
	        rCntlInfo.getOperatorKey().c_str(),
	        rCntlInfo.getOperatorClientIP());
	
		if(const_cast<CCntlInfo&>(rCntlInfo).isRouteKeySet())	
			oCgiStubCntl.setUin(const_cast<CCntlInfo&>(rCntlInfo).getRouteKey());	
	    
	    int iRetCode = 0;
		m_sErrMsg.clear();
	    if((iRetCode = oCgiStubCntl.putSvcObj(&oReq, 0)) != 0)
	    {
			m_sErrMsg = "DoRequest failed:";
			m_sErrMsg += CCGI_STUB_CNTL->GetLastErrMsg();
	        return iRetCode;
	    }
	    
	    // Send & Recv
		if((iRetCode = SendRecv()) != 0)
		{
			if(99 == iRetCode) // UDP & Need No Response
				return 0;
	
			std::stringstream oss;
			oss << m_lTimeout;
	        m_sErrMsg = "Send & Recv Timeout(";
			m_sErrMsg += oss.str();
			m_sErrMsg += "s) ";
			m_sErrMsg += oCgiStubCntl.GetLastErrMsg();
			return iRetCode;
	    }
	    
		if(!m_poCmd->isValid(COMPANYDAO_GETCOMPANY_RESP_CMD_ID))
	    {
	        std::stringstream oss;
            oss << "Cmd not match: "
                << "Expected cmd=" << std::hex << COMPANYDAO_GETCOMPANY_RESP_CMD_ID << std::dec << " != "
                << "actualed cmd=" << std::hex << m_poCmd->getCmdId() << std::dec;
            m_sErrMsg = oss.str();
        
	        return 0xEE0024;
	    }
	    
	    char* pBuf = NULL; uint32_t nLen = 0;
	    m_poCmd->getBuffer(pBuf, nLen);
	    CByteStreamNetwork bs(pBuf, nLen);
	    bs.isStoring(false);
	    
	    CCompanyDaoGetCompanyResp oResp;
	    if(!oResp.Serialize(bs))
	    {
	        m_sErrMsg = "Resp.Serialize(bs) failed";
	        return 0xEE0028;
	    }
	    
			errmsg  = oResp.errmsg;
			companyInfo  = oResp.companyInfo;
			outReserve  = oResp.outReserve;
	    
	    return oResp.dwResult;
	}
	uint32_t CCompanyDaoStub4Web::GetCompanyByIds(
				const CCntlInfo& rCntlInfo,
				const std::string& machineKey,
				const std::string& source,
				uint32_t sceneId,
				const std::set<uint32_t >& companysysNoList,
				const std::string& inReserve,
				std::string& errmsg,
				std::map<uint32_t,erp::company::ddo::CCompanyInfoDdo >& companyInfo,
				std::string& outReserve
	)
	{
		CCgiStubCntl oCgiStubCntl;
		oCgiStubCntl.Initialize_r();
		this->SetCgiStubCntl(&oCgiStubCntl);
	
	    CCompanyDaoGetCompanyByIdsReq oReq;
			oReq.machineKey = machineKey;
			oReq.source = source;
			oReq.sceneId = sceneId;
			oReq.companysysNoList = companysysNoList;
			oReq.inReserve = inReserve;
			oReq.errmsg = errmsg;
			oReq.companyInfo = companyInfo;
			oReq.outReserve = outReserve;

		// oCgiStubCntl.setRouteKey(uint32_t routeKey); 娌℃湁璁剧疆routeKey  
	
	    oCgiStubCntl.setPassport(
	        rCntlInfo.getOperatorUin(), 
	        rCntlInfo.getOperatorKey().c_str(),
	        rCntlInfo.getOperatorClientIP());
	
		if(const_cast<CCntlInfo&>(rCntlInfo).isRouteKeySet())	
			oCgiStubCntl.setUin(const_cast<CCntlInfo&>(rCntlInfo).getRouteKey());	
	    
	    int iRetCode = 0;
		m_sErrMsg.clear();
	    if((iRetCode = oCgiStubCntl.putSvcObj(&oReq, 0)) != 0)
	    {
			m_sErrMsg = "DoRequest failed:";
			m_sErrMsg += CCGI_STUB_CNTL->GetLastErrMsg();
	        return iRetCode;
	    }
	    
	    // Send & Recv
		if((iRetCode = SendRecv()) != 0)
		{
			if(99 == iRetCode) // UDP & Need No Response
				return 0;
	
			std::stringstream oss;
			oss << m_lTimeout;
	        m_sErrMsg = "Send & Recv Timeout(";
			m_sErrMsg += oss.str();
			m_sErrMsg += "s) ";
			m_sErrMsg += oCgiStubCntl.GetLastErrMsg();
			return iRetCode;
	    }
	    
		if(!m_poCmd->isValid(COMPANYDAO_GETCOMPANYBYIDS_RESP_CMD_ID))
	    {
	        std::stringstream oss;
            oss << "Cmd not match: "
                << "Expected cmd=" << std::hex << COMPANYDAO_GETCOMPANYBYIDS_RESP_CMD_ID << std::dec << " != "
                << "actualed cmd=" << std::hex << m_poCmd->getCmdId() << std::dec;
            m_sErrMsg = oss.str();
        
	        return 0xEE0024;
	    }
	    
	    char* pBuf = NULL; uint32_t nLen = 0;
	    m_poCmd->getBuffer(pBuf, nLen);
	    CByteStreamNetwork bs(pBuf, nLen);
	    bs.isStoring(false);
	    
	    CCompanyDaoGetCompanyByIdsResp oResp;
	    if(!oResp.Serialize(bs))
	    {
	        m_sErrMsg = "Resp.Serialize(bs) failed";
	        return 0xEE0028;
	    }
	    
			errmsg  = oResp.errmsg;
			companyInfo  = oResp.companyInfo;
			outReserve  = oResp.outReserve;
	    
	    return oResp.dwResult;
	}

